/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.gallery;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cenluan.common.OssManager;
import com.cenluan.gallery.member.GalleryMember;
import com.cenluan.kit.XssKit;
import com.cenluan.photo.Photo;
import com.cenluan.user.User;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 相册
 * 
 * @author peak
 * 
 */
public class Gallery extends Model<Gallery> {

	private static final long serialVersionUID = 1L;

	public static final Gallery dao = new Gallery();
	// 相册状态值，不用布尔类型是因为可能会有别的值
	public static final String STATE_ACTIVE = "ACTIVE";
	public static final String STATE_DELETED = "DELETED";

	/**
	 * 获取创建者
	 * 
	 * @return
	 */
	public User getCreater() {
		return User.dao.findById(getStr("createrId"));
	}

	/**
	 * 获取封面图片
	 * 
	 * @return
	 */
	public Photo getCover() {
		return Photo.dao.findById(getStr("coverId"));
	}

	/**
	 * 获取html代码，返回一个链接，文本是相册标题，地址是相册详情信息
	 * 
	 * @return
	 */
	public String getHtml() {
		return "<a href='/gallery/show/" + getStr("id") + "'>"
				+ XssKit.escapeStr(getStr("title")) + "</a>";
	}

	/**
	 * 获取封面地址
	 * 
	 * @return
	 */
	public String getCoverUrl() {
		if (getStr("coverId") != null) {
			return Photo.dao.findById(getStr("coverId")).getUrl();
		} else {
			return OssManager.getImageUrl("default-gallery-cover.jpg");
		}
	}

	/**
	 * 获取照片数量
	 * 
	 * @return
	 */
	public long getPhotoCount() {
		return Db.queryLong("select count(*) from t_photo where galleryId=?",
				getStr("id"));
	}

	/**
	 * 获取最后更新时间，即最后上传照片的时间
	 * 
	 * @return
	 */
	public Date getLastUpdateTime() {
		return Db.queryDate(
				"select max(createTime) from t_photo where galleryId=?",
				getStr("id"));
	}

	/**
	 * 分页查询
	 * 
	 * @param pn
	 * @param pz
	 * @param title
	 * @param createrId
	 * @param order
	 *            1.new按照创建时间倒序,默认排序.2.pc按照片数量倒序排列.
	 * @return
	 */
	public Page<Gallery> page(int pn, int pz, String title, String createrId,
			String order) {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_gallery g ");
		if ("pc".equals(order)) {
			sql.append("left join ( ");
			sql.append(" select count(*) ct,galleryId ");
			sql.append(" from t_photo group by galleryId ");
			sql.append(" )s on s.galleryId=g.id ");
		}
		sql.append(" where g.state=? ");
		paras.add(STATE_ACTIVE);
		if (StrKit.notBlank(title)) {
			sql.append(" and g.title like ? ");
			paras.add("%" + title + "%");
		}
		if (StrKit.notBlank(createrId)) {
			sql.append(" and g.createrId=? ");
			paras.add(createrId);
		}
		if ("pc".equals(order)) {
			sql.append(" order by s.ct desc ");
		} else {
			sql.append(" order by g.createTime desc ");
		}
		return paginate(pn, pz, "select g.*", sql.toString(), paras.toArray());
	}

	/**
	 * 成员数量
	 * 
	 * @return
	 */
	public long getMemberCount() {
		return Db.queryLong("select count(*) from t_gallery_member "
				+ "where galleryId=? and active=1", getStr("id"));
	}

	/**
	 * 获取成员列表
	 * 
	 * @return
	 */
	public List<GalleryMember> getMemberList() {
		StringBuilder sql = new StringBuilder();
		sql.append(" select * ");
		sql.append(" from t_gallery_member ");
		sql.append(" where active=1 and galleryId=? ");
		sql.append(" order by joinTime desc ");
		return GalleryMember.dao.find(sql.toString(), getStr("id"));

	}

	/**
	 * 获取被禁的成员列表
	 * 
	 * @return
	 */
	public List<GalleryMember> getDisabledMemberList() {
		StringBuilder sql = new StringBuilder();
		sql.append(" select * ");
		sql.append(" from t_gallery_member ");
		sql.append(" where active=0 and galleryId=? ");
		sql.append(" order by joinTime desc ");
		return GalleryMember.dao.find(sql.toString(), getStr("id"));
	}

}
